# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Kevin Daudt <kdaudt@alpinelinux.org>
pkgname=netdata
pkgver=1.45.4
pkgrel=0
pkgdesc="Real-time performance monitoring"
url="https://www.netdata.cloud/"
license="GPL-3.0-or-later"
arch="all"
depends="bash coreutils"
makedepends="
	abseil-cpp-dev
	brotli-dev
	cmake
	cups-dev
	e2fsprogs-dev
	elfutils-dev
	go
	json-c-dev
	judy-dev
	libcap-dev
	libmnl-dev
	libnetfilter_acct-dev
	libuuid
	libuv-dev
	linux-headers
	lz4-dev
	samurai
	openssl-dev>3
	protobuf-dev
	yaml-dev
	zlib-dev
	"
checkdepends="cmocka-dev"
pkgusers="netdata"
pkggroups="netdata"
subpackages="$pkgname-openrc $pkgname-python::noarch $pkgname-go-plugins:go"
install="$pkgname.pre-install"
options="!check" # tests require installation to /
_aclk_schemas_commit=83c661c0dcddb9526814ebbd0668fbc3e281f03f
_ml_dlib_commit=021cbbb1c2ddec39d8dd4cb6abfbbafdf1cf4482
# This _h2o_commit fixes CVE-2023-44487, and does not follow upstream
_h2o_commit=094a6f90f76eeb280a430200cba47bde39b0b8e8
_fluentbit_commit=b19e9ce674de872640c00a697fa545b66df0628a

source="$pkgname-$pkgver.tar.gz::https://github.com/netdata/netdata/archive/v$pkgver.tar.gz
	aclk-schemas-$_aclk_schemas_commit.tar.gz::https://github.com/netdata/aclk-schemas/archive/$_aclk_schemas_commit.tar.gz
	dlib-$_ml_dlib_commit.tar.gz::https://github.com/davisking/dlib/archive/$_ml_dlib_commit.tar.gz
	h2o-$_h2o_commit.tar.gz::https://github.com/h2o/h2o/archive/$_h2o_commit.tar.gz
	fluent-bit-$_fluentbit_commit.tar.gz::https://github.com/fluent/fluent-bit/archive/$_fluentbit_commit.tar.gz

	openrc-pidfile-not-writable-by-user.patch

	protobuf-23.patch
	build-enable-cgo.patch
	"

# secfixes:
#   1.43.2-r1:
#     - CVE-2023-44487
#   0:
#     - CVE-2024-32019

_freeipmi=False
case $CARCH in
	ppc64le) ;; # no freeipmi
	*)
		makedepends="$makedepends freeipmi-dev"
		_freeipmi=True
	;;
esac

copy_submodule() {
	local name=$1
	local commit=$2
	local dest=$3

	cp -r \
		$srcdir/$name-$commit \
		-T "$builddir"/$dest

}

prepare() {
	copy_submodule aclk-schemas "$_aclk_schemas_commit" src/aclk/aclk-schemas
	copy_submodule dlib "$_ml_dlib_commit" src/ml/dlib
	copy_submodule h2o "$_h2o_commit" src/web/server/h2o/libh2o
	copy_submodule fluent-bit "$_fluentbit_commit" src/fluent-bit

	default_prepare
}

build() {
	local cmake_crossopts
	if [ "$CBUILD" != "$CHOST" ]; then
		cmake_crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi

	# ENABLE_EXPORTER_PROMETHEUS_REMOTE_WRITE: snappy not found due to lack of
	# pkgconf
	# ENABLE_PLUGIN_EBPF: include files missing
	# ENABLE_LOGS_MANAGEMENT_TESTS: tries to assign read-only stdout/stderr
	cmake -G Ninja -B build \
		-DCMAKE_INSTALL_PREFIX=/ \
		-DCMAKE_BUILD_TYPE=Release \
		-DWEB_DIR=/usr/share/webapps/netdata \
		-DENABLE_PLUGIN_SYSTEMD_JOURNAL=False \
		-DENABLE_PLUGIN_XENSTAT=False \
		-DENABLE_PLUGIN_CUPS=False \
		-DENABLE_PLUGIN_EBPF=False \
		-DENABLE_PLUGIN_GO=True \
		-DENABLE_PLUGIN_FREEIPMI=$_freeipmi \
		-DENABLE_EXPORTER_PROMETHEUS_REMOTE_WRITE=False \
		-DENABLE_EXPORTER_MONGODB=False \
		-DENABLE_LOGS_MANAGEMENT_TESTS=False \
		$cmake_crossopts
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	chgrp -R $pkggroups "$pkgdir"/usr/share/webapps/netdata
	install -Dm755 build/system/openrc/init.d/netdata -t $pkgdir/etc/init.d/
	install -Dm644 build/system/openrc/conf.d/netdata -t $pkgdir/etc/conf.d/
	install -Dm644 ./system/netdata.conf $pkgdir/etc/netdata/netdata.conf

	# auxilary files for various systems, but not installed at the correct
	# location
	rm -r "$pkgdir"/usr/lib/netdata/system

	# Fix permissions
	chown -R netdata: \
		"$pkgdir"/var/cache/netdata \
		"$pkgdir"/var/lib/netdata \
		"$pkgdir"/var/log/netdata
	chmod 0750 "$pkgdir"/var/cache/netdata
	chmod 0750 "$pkgdir"/var/lib/netdata

	# rundir in /run
	rmdir "$pkgdir"/var/run
	install -dm0755 "$pkgdir"/run/netdata
}

python() {
	pkgdesc="$pkgdesc (python3 support)"
	depends="$pkgname python3"

	amove usr/libexec/netdata/python.d
	amove usr/libexec/netdata/plugins.d/python.d.plugin
	amove etc/netdata/python.d
	amove usr/lib/netdata/conf.d/python.d
	amove usr/lib/netdata/conf.d/python.d.conf
}

go() {
	pkgdesc="$pkgdesc (go plugins)"
	depends="$pkgname"

	amove usr/libexec/netdata/plugins.d/go.d.plugin
	amove usr/lib/netdata/conf.d/go.d.conf
	amove usr/lib/netdata/conf.d/go.d
	amove usr/lib/netdata/conf.d/health.d/go.d.plugin.conf
	amove etc/netdata/go.d
}

sha512sums="
440ba2ea11480a265507db17d09fabb8864d2a6c87bda98a13457994c95bf393fee099ff430dcd18053e4ef31c9e8d9b01d1eed0c6a433214296953b6c186399  netdata-1.45.4.tar.gz
9687ede999002e73563c118fb604cbf8957670f8fac8f7c51d637c1518c00c1919e3b66bf055f152cda766695fc625a751db19a91174f487c0cc2752d47afb52  aclk-schemas-83c661c0dcddb9526814ebbd0668fbc3e281f03f.tar.gz
814e3ee5811b43349a218c2b6595e53c1daf5db2c9346b2ec0ff6475cc83b1803adad3f3210f51e219d6523fcf839543087b94eb18501ee144128fb169bd9c0b  dlib-021cbbb1c2ddec39d8dd4cb6abfbbafdf1cf4482.tar.gz
f494a2bf75fe7a6c97bf64ae8d2a4eabe6bcc5c1a4a038cb27c23b478a65a543f9c2dcc8223b0e05ebe2ee811baa06876d13d5abb344c1bc51bdf5e3e8d3a75b  h2o-094a6f90f76eeb280a430200cba47bde39b0b8e8.tar.gz
e2cc42781b668a102ca73c49790e652b5daf42999ae7c711e2fffc59b6e556bee2d0008dafbc1746b7a7cebe00ce4b8ee30b7327b5cc58df2575919f76daa442  fluent-bit-b19e9ce674de872640c00a697fa545b66df0628a.tar.gz
bfa455511b582497c358be5a9408f066495edfade229b7971c5de1a85a8b7a5a503b474c9341eb8f367dc1f70be9663186c978ecc85280096f928941c002671d  openrc-pidfile-not-writable-by-user.patch
c42a973ad1c7bb7b16a0c0d20580ac168071d37c06ad6b3336388ba7c5d59dd8b90795ea9a7f501d01755062d0a9a405a61b49f05a940c942c2154400d2721ee  protobuf-23.patch
2f7b5f6a91fac0bd0a9a0a8ed4a53eb74db8f296332e35ca293f5a916fa6e58f2ebc32fd6621d41de68d61d31d1c85b5567c2b0800c7a484aed03cf1b89472eb  build-enable-cgo.patch
"
